package com.whlilong.jxc.view;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.part.ViewPart;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.input.StorageMangerEditorInput;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.util.ViewerChange;

/**
 * 库存管理视图
 * 
 * @author sjy
 * 
 */
public class StoreManagerView extends ViewPart implements ISelectionListener {

	private CLabel rkdj; // 入库登记
	private CLabel kcgl; // 库存管理
	private CLabel ckdj; // 出库登记
	private CLabel rkdetail; // 入库明细
	private CLabel ckdetail; // 出库明细
	private CLabel xline; // 线条

	private final Color blue = Display.getCurrent().getSystemColor(
			SWT.COLOR_BLUE);
	private final Color black = Display.getCurrent().getSystemColor(
			SWT.COLOR_BLACK);
	private final IWorkbenchWindow window = PlatformUI.getWorkbench()
			.getActiveWorkbenchWindow();

	public StoreManagerView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		createContent(parent);
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.addSelectionListener(this);
	}

	private void createContent(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 5;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		layout.marginRight = 10;
		layout.marginLeft = 10;
		layout.makeColumnsEqualWidth = false;

		composite.setLayout(layout);
		composite.setBackgroundImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.FUNCTIONS_BG));

		createFlowchart(composite);
	}

	private void createFlowchart(Composite parent) {
		// TODO Auto-generated method stub

		rkdj = new CLabel(parent, SWT.NONE);
		GridData gd = new GridData(GridData.CENTER);
		gd.widthHint = 120;
		gd.heightHint = 100;
		rkdj.setText("商品入库登记");
		rkdj.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		rkdj
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		rkdj.setLayoutData(gd);

		xline = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.horizontalIndent = 0;
		gd.verticalSpan = 2;
		gd.widthHint = 60;
		gd.heightHint = 200;
		xline.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.xline));
		xline.setBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_WHITE));
		xline.setLayoutData(gd);

		kcgl = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalSpan = 2;
		gd.widthHint = 100;
		gd.heightHint = 200;
		kcgl.setText("库存管理");
		kcgl.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.MAICHANGGUANLI_ICON));
		kcgl
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		kcgl.setLayoutData(gd);

		xline = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.horizontalIndent = 0;
		gd.verticalSpan = 2;
		gd.widthHint = 60;
		gd.heightHint = 200;
		xline.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.yline));
		xline.setBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_WHITE));
		xline.setLayoutData(gd);

		ckdj = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.widthHint = 120;
		gd.heightHint = 100;
		ckdj.setText("商品出库登记");
		ckdj.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		ckdj
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		ckdj.setLayoutData(gd);

		rkdetail = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.widthHint = 120;
		gd.heightHint = 100;
		rkdetail.setText("商品入库明细");
		rkdetail.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		rkdetail.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		rkdetail.setLayoutData(gd);

		ckdetail = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.widthHint = 120;
		gd.heightHint = 100;

		ckdetail.setText("商品出库明细");
		ckdetail.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		ckdetail.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		ckdetail.setLayoutData(gd);

		addListener();
	}

	private void addListener() {
		// TODO Auto-generated method stub
		rkdj.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				rkdj.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				rkdj.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				rkdj.setToolTipText(rkdj.getText());
			}

		});
		rkdetail.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				rkdetail.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				rkdetail.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				rkdetail.setToolTipText(rkdetail.getText());
			}

		});
		kcgl.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				kcgl.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				kcgl.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				kcgl.setToolTipText(kcgl.getText());
			}

		});
		kcgl.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				StorageMangerEditorInput input = new StorageMangerEditorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.storageManagerEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		ckdj.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				ckdj.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				ckdj.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				ckdj.setToolTipText(ckdj.getText());
			}

		});
		ckdetail.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				ckdetail.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				ckdetail.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				ckdetail.setToolTipText(ckdetail.getText());
			}

		});

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (!selection.isEmpty()) {
			IViewPart vp = getViewSite().getPage().findView(
					IAppConstants.systemManagerview_id);

			Table table = ((SystemManagerView) vp).getTableViewer().getTable();
			ViewerChange.getInstance().refreshView(table);
		}
	}

}
